/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;

namespace ELE.EnterpriseLibrary.Data
{
  /// <summary>
  /// A wrapper for <see cref="ObjectSqlQueryManagerFactory"/> and <see cref="ObjectSqlMappingManagerFactory"/>
  /// </summary>
  public static class ObjectSqlFactory
  {
    private static ObjectStoredProcedureManagerFactory storedProcedureFactory = new ObjectStoredProcedureManagerFactory(ConfigurationSourceFactory.Create());
    private static ObjectSqlQueryManagerFactory queryFactory = new ObjectSqlQueryManagerFactory(ConfigurationSourceFactory.Create());
    private static ObjectSqlMappingManagerFactory mappingFactory = new ObjectSqlMappingManagerFactory(ConfigurationSourceFactory.Create());
    private static object lockObject = new object();
    /// <summary>
    /// Initializes the factories from a configuration source that is a app config type file.
    /// </summary>
    /// <param name="path"></param>
    public static void InitializeSource(string path)
    {
      IConfigurationSource file = new FileConfigurationSource(path);
      if (file != null)
      {
        storedProcedureFactory = new ObjectStoredProcedureManagerFactory(file);
        queryFactory = new ObjectSqlQueryManagerFactory(file);
        mappingFactory = new ObjectSqlMappingManagerFactory(file);
      }
    }
    /// <summary>
    /// Gets the manager for the mapping
    /// </summary>
    /// <returns></returns>
    public static ObjectSqlMappingManager GetObjectSqlMappingManager()
    {
      try
      {
        lock (lockObject)
        {
          return mappingFactory.CreateDefault();
        }
      }
      catch (ConfigurationErrorsException configurationException)
      {
        TryLogConfigurationError(configurationException, "default");

        throw;
      }
    }
    /// <summary>
    /// Gets the manager for the mapping by name
    /// </summary>
    /// <param name="managerName"></param>
    /// <returns></returns>
    public static ObjectSqlMappingManager GetObjectSqlMappingManager(string managerName)
    {
      try
      {
        lock (lockObject)
        {
          return mappingFactory.Create(managerName);
        }
      }
      catch (ConfigurationErrorsException configurationException)
      {
        TryLogConfigurationError(configurationException, managerName);

        throw;
      }
    }

    /// <summary>
    /// Get the manager for queries
    /// </summary>
    /// <returns></returns>
    public static ObjectSqlQueryManager GetObjectSqlQueryManager()
    {
      try
      {
        lock (lockObject)
        {
          return queryFactory.CreateDefault();
        }
      }
      catch (ConfigurationErrorsException configurationException)
      {
        TryLogConfigurationError(configurationException, "default");

        throw;
      }
    }
    /// <summary>
    /// Gets the manager for queries by name.
    /// </summary>
    /// <param name="managerName"></param>
    /// <returns></returns>
    public static ObjectSqlQueryManager GetObjectSqlQueryManager(string managerName)
    {
      try
      {
        lock (lockObject)
        {
          return queryFactory.Create(managerName);
        }
      }
      catch (ConfigurationErrorsException configurationException)
      {
        TryLogConfigurationError(configurationException, managerName);

        throw;
      }
    }
    /// <summary>
    /// Manager for stored procedures
    /// </summary>
    /// <param name="managerName"></param>
    /// <returns></returns>
    public static ObjectStoredProcedureManager GetObjectStoredProcedureManager(string managerName)
    {
      try
      {
        lock (lockObject)
        {
          return storedProcedureFactory.Create(managerName);
        }
      }
      catch (ConfigurationErrorsException configurationException)
      {
        TryLogConfigurationError(configurationException, managerName);

        throw;
      }
    }

    private static void TryLogConfigurationError(ConfigurationErrorsException configurationException, string instanceName)
    {
    }
  }
}
